﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Technoera.Common.Interop.OleDb
{
    public class ParseBooleanImportMapping : IImportMapping
    {
        public ParseBooleanImportMapping(string sourceColumnName, PropertyInfo targetPropertyInfo, string trueValueString)
        {
            SourceColumnName = sourceColumnName;
            TargetPropertyInfo = targetPropertyInfo;
            TrueValueString = trueValueString;
        }

        private string _sourceColumnName;
        public string SourceColumnName
        {
            get
            {
                if (_sourceColumnName == null)
                    _sourceColumnName = string.Empty;

                return _sourceColumnName;
            }
            set
            {
                _sourceColumnName = value;
            }
        }

        public PropertyInfo TargetPropertyInfo { get; set; }

        private string _trueValueString;
        public string TrueValueString
        {
            get
            {
                if (_trueValueString == null)
                    _trueValueString = string.Empty;

                return _trueValueString;
            }
            set
            {
                _trueValueString = value;
            }
        }
	

        #region IImportMapping Members
        public void SetPropertyValue(System.Data.IDataReader dataReader, object instance)
        {
            object sourceValue = dataReader[SourceColumnName];
            if (sourceValue is System.DBNull) return;
            TargetPropertyInfo.SetValue(instance, TrueValueString.Equals((string)sourceValue, StringComparison.InvariantCultureIgnoreCase), null);
        }
        #endregion
    }
}
